// pages/qna/paper.js
const conf = require('../../app.config')
const app =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cdn: conf.app.cdnPath,
    v: conf.app.v,
    qanow: 0,
    opts: 'ABCDEFGHIJK',
    tips: { phoneAuth: false, userAuth: false },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.api.interface.gettitle().then(res=>{
      console.log(res,111111111)
      if (res.data.code == 200) {
        this.setData({ qalist: res.data.data })
        let ars = []
        for (var i = 0; i < res.data.data.list.length; i++)
            ars.push(new Array())
        this.setData({ answerResult: ars })
    }
    })
  },
  onAnswer(e) {
    let { qa, key } = e.currentTarget.dataset
    let asw = this.data.qalist
    if (asw.list[qa].multiple)
        asw.list[qa].items[key].selected = asw.list[qa].items[key].selected ? false : true
    else {
        for (var i = 0; i < asw.list[qa].items.length; i++)
            asw.list[qa].items[i].selected = false
        asw.list[qa].items[key].selected = true
    }
    this.setData({ qalist: asw })
  },
  onNext() {
    let chk = false,
        list = this.data.qalist.list[this.data.qanow]
    for (var i = 0; i < list.items.length; i++) {
        if (list.items[i].selected) {
            chk = true
            break
        }
    }
    if (chk) {
        if (this.data.qanow + 1 < this.data.qalist.list.length)
            this.setData({ qanow: this.data.qanow + 1 })
        else {
            let parm = { list: [] },
                list = this.data.qalist.list
            for (var i = 0; i < list.length; i++) {
                let aws = new Array()
                for (var x = 0; x < list[i].items.length; x++) {
                    if (list[i].items[x].selected)
                        aws.push(list[i].items[x])
                }
                parm.list.push({ typeCode: list[i].typeCode, items: aws })
            }
            app.loading.show('答案提交中')
            wx.setStorageSync('answerlist', parm)
            // 如果为501 则为老板用户已授权 无需走授权流程 直接到答题结果页
            if(wx.getStorageSync('logincode') == 501 || wx.getStorageSync('userinfo').nickName){
              wx.navigateTo({
                url: '../qna/result',
              })
            }else{
              wx.navigateTo({
                url: '/pages/authorization/authorization',
              })
            }
        }
    }
  },
  onPageScroll(e) {
    if(e.scrollTop.data.code=200){
      wx.reLaunch({
        url: '../home/home',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})